#include "stdio.h"
#include "stdlib.h"
#include "moudle.h"
//here copyright fuhao
//struct date* rec(void);
//struct date* save(struct date* top);
//int showmenu(void);
struct date* rec(void){//recover data
FILE *fp;
	char adr[20];
	struct date* top;//new top pointer
	top=(pointer)malloc(sizeof(datedef));
	printf("Input file name or URL>>>");
	scanf("%s",&adr);
	//gets(adr);
	//printf("%s",adr);
	fp=fopen(adr,"r");
	if(fp==NULL){
        printf("ERROR!The file open FAILS\n");
        rec();// ---------return to menu  to be built
    }
    //fscanf(fp,"%c",&top->ao);
   // printf("%c",top->ao);
    // return 0;
    fscanf(fp,"%s %d %s %d %f\n",&top->ao,&top->num,&top->pat_name,&top->age,&top->cost);
    struct date* q;
    printf("Restoring %s\n=>",adr);
    q=top;
    //pr=top;
    while(!feof(fp)){//top pointer to next
        struct date* pr;
        pr=(pointer)malloc(sizeof(datedef));
        fscanf(fp,"%s %d %s %d %f\n",&pr->ao,&pr->num,&pr->pat_name,&pr->age,&pr->cost);
        q->next=pr;
        q=q->next;
        int i;
        for(i=0;i<1e6;i++)//count longer
            {;}
        printf("=>");//cool UI
    }
    printf("\nRestored!\n");
    return top;
}
struct date* save(struct date* top){
    char adr[20];
    int i;
    FILE *fp;
    printf("NOW SAVE THE DATA AS (file name or URL)>>>");
    //getchar();//if diaoyong press enter
    gets(adr);
    fp=fopen(adr,"w");//-------is adr or  'adr'!!!!!!!!!!
    if(fp==NULL){
        printf("ERROR!new file open FAILS\n");
        save(top);// ---------return to menu  to be built
    }
    printf("Saving\n");
    while(top!=NULL){//top pointer to next
        fprintf(fp,"%c %d %s %d %.2f\n",top->ao,top->num,top->pat_name,top->age,top->cost);
        top=top->next;
        printf("=>");

        for(i=0;i<1e6;i++)//count longer
            {;}
    }
    fclose(fp);
    printf("\nData Save Successfully!\n");
}
//int showmenu(void){

//}
